<?php
//By Bing Hua
include_once 'functions.php';
include_once 'insert_functions.php';
$date = $_POST ['date'];
$cropNameWithUnit1 = $_POST ['cropNameWithUnit1'];
$cropNameWithUnit2 = $_POST ['cropNameWithUnit2'];
$cropNameWithUnit3 = $_POST ['cropNameWithUnit3'];
$cropNameWithUnit4 = $_POST ['cropNameWithUnit4'];
$cropNum1 = $_POST ['cropNum1'];
$cropNum2 = $_POST ['cropNum2'];
$cropNum3 = $_POST ['cropNum3'];
$cropNum4 = $_POST ['cropNum4'];
$customer1 = $_POST ['customer1'];
$customer2 = $_POST ['customer2'];
$customer3 = $_POST ['customer3'];
$customer4 = $_POST ['customer4'];
$cropName1 = getCropName ( $cropNameWithUnit1 );
$unit1 = getCropUnit ( $cropNameWithUnit1 );
$cropName2 = getCropName ( $cropNameWithUnit2 );
$unit2 = getCropUnit ( $cropNameWithUnit2 );
$cropName3 = getCropName ( $cropNameWithUnit3 );
$unit3 = getCropUnit ( $cropNameWithUnit3 );
$cropName4 = getCropName ( $cropNameWithUnit4 );
$unit4 = getCropUnit ( $cropNameWithUnit4 );

$ok = true;
$msg = "";

if ($cropNum1 != 0) {
	if (checkSDLog ( getCropId ( $cropName1 ), $date, getUnitId ( $unit1 ), 1, getCustomerId ( $customer1 ) )) {
		$ok = false;
		$msg = $msg . "Overwrite existing Donation Data on " . $cropNameWithUnit1 . "! Quantity is " . $cropNum1 . " Date is " . $date . " and Customer is " . $customer1 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Donation Data on " . $cropNameWithUnit1 . "! Quantity is " . $cropNum1 . " Date is " . $date . " and Customer is " . $customer1 . "." . '\n' . '\n';
	}
}
if ($cropNum2 != 0) {
	if (checkSDLog ( getCropId ( $cropName2 ), $date, getUnitId ( $unit2 ), 1, getCustomerId ( $customer2 ) )) {
		$ok = false;
		$msg = $msg . "Overwrite existing Donation Data on " . $cropNameWithUnit2 . "! Quantity is " . $cropNum2 . " Date is " . $date . " and Customer is " . $customer2 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Donation Data on " . $cropNameWithUnit2 . "! Quantity is " . $cropNum2 . " Date is " . $date . " and Customer is " . $customer2 . "." . '\n' . '\n';
	}
}
if ($cropNum3 != 0) {
	if (checkSDLog ( getCropId ( $cropName3 ), $date, getUnitId ( $unit3 ), 1, getCustomerId ( $customer3 ) )) {
		$ok = false;
		$msg = $msg . "Overwrite existing Donation Data on " . $cropNameWithUnit3 . "! Quantity is " . $cropNum3 . " Date is " . $date . " and Customer is " . $customer3 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Donation Data on " . $cropNameWithUnit3 . "! Quantity is " . $cropNum3 . " Date is " . $date . " and Customer is " . $customer3 . "." . '\n' . '\n';
	}
}
if ($cropNum4 != 0) {
	if (checkSDLog ( getCropId ( $cropName4 ), $date, getUnitId ( $unit4 ), 1, getCustomerId ( $customer4 ) )) {
		$ok = false;
		$msg = $msg . "Overwrite existing Donation Data on " . $cropNameWithUnit4 . "! Quantity is " . $cropNum4 . " Date is " . $date . " and Customer is " . $customer4 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Donation Data on " . $cropNameWithUnit4 . "! Quantity is " . $cropNum4 . " Date is " . $date . " and Customer is " . $customer4 . "." . '\n' . '\n';
	}
}
if ($ok == true) {
	if ($cropNum1 != 0)
		addSDLog ( getCropId ( $cropName1 ), $cropNum1, $date, getUnitId ( $unit1 ), null, 1, getCustomerId ( $customer1 ) );
	if ($cropNum2 != 0)
		addSDLog ( getCropId ( $cropName2 ), $cropNum2, $date, getUnitId ( $unit2 ), null, 1, getCustomerId ( $customer2 ) );
	if ($cropNum3 != 0)
		addSDLog ( getCropId ( $cropName3 ), $cropNum3, $date, getUnitId ( $unit3 ), null, 1, getCustomerId ( $customer3 ) );
	if ($cropNum4 != 0)
		addSDLog ( getCropId ( $cropName4 ), $cropNum4, $date, getUnitId ( $unit4 ), null, 1, getCustomerId ( $customer4 ) );
	
	echo "<script type='text/javascript'>alert('";
	echo $msg;
	echo "')</script>";
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=insert_donation.php">';
} else {
	echo '<script language="JavaScript">';
	$msg = $msg . 'Are you sure?' . '\n';
	echo 'if(confirm(\'' . $msg . '\'))
		location.href = "insert_donation_process_sure.php?date=' . $date . '&cropNameWithUnit1=' . $cropNameWithUnit1 . '&cropNameWithUnit2=' . $cropNameWithUnit2 . '&cropNameWithUnit3=' . $cropNameWithUnit3 . '&cropNameWithUnit4=' . $cropNameWithUnit4 . '&cropNum1=' . $cropNum1 . '&cropNum2=' . $cropNum2 . '&cropNum3=' . $cropNum3 . '&cropNum4=' . $cropNum4 . '&customer1=' . $customer1 . '&customer2=' . $customer2 . '&customer3=' . $customer3 . '&customer4=' . $customer4 . '"; else
		location.href = "insert_donation.php?date=' . $date . '&cropNameWithUnit1=' . $cropNameWithUnit1 . '&cropNameWithUnit2=' . $cropNameWithUnit2 . '&cropNameWithUnit3=' . $cropNameWithUnit3 . '&cropNameWithUnit4=' . $cropNameWithUnit4 . '&cropNum1=' . $cropNum1 . '&cropNum2=' . $cropNum2 . '&cropNum3=' . $cropNum3 . '&cropNum4=' . $cropNum4 . '&customer1=' . $customer1 . '&customer2=' . $customer2 . '&customer3=' . $customer3 . '&customer4=' . $customer4 . '";
		</script>';
}
?>